package com.airconditioner.sales.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.airconditioner.sales.R;
import com.airconditioner.sales.service.IntentService;
import com.airconditioner.sales.util.CheckNetwork;
import com.airconditioner.sales.util.ShareP;
import com.igexin.sdk.PushManager;
import com.igexin.sdk.PushService;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


public class BaseActivity extends Activity {
    public Context context;
    private ProgressDialog progressDialog;
    public ImageView title_iv_back;
    public TextView title_tv_title, title_tv_right;
    public RelativeLayout title_relate_layout;
    public ImageView title_iv_right;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        x.view().inject(this);
        init();
    }

    public void init() {
        context = this;
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidthDip = dm.widthPixels; // 屏幕宽
        int screenHeightDip = dm.heightPixels; // 屏幕宽
        ShareP.saveWidth(context, screenWidthDip);
        ShareP.saveHeight(context, screenHeightDip);
        title_iv_back = (ImageView) findViewById(R.id.title_iv_back);
        title_relate_layout = (RelativeLayout) findViewById(R.id.title_layout);
        title_tv_title = (TextView) findViewById(R.id.title_title);
        title_iv_right = (ImageView) findViewById(R.id.title_right);
        title_tv_right = (TextView) findViewById(R.id.title_tv_right);
    }

    public void http(String url, final String type) {
        if (CheckNetwork.isNetworkAvailable(BaseActivity.this)) {
            showDia("正在加载中...");
            RequestParams params = new RequestParams(url);
            x.http().get(params, new Callback.CacheCallback<String>() {
                @Override
                public boolean onCache(String result) {
                    return false;
                }

                @Override
                public void onSuccess(String result) {
                    success(result, type);
                    hideDia();
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    error(ex.getMessage(), type);
                    System.out.println(ex.getLocalizedMessage());
                    hideDia();
                }

                @Override
                public void onCancelled(CancelledException cex) {
//                    hideDia();
                }

                @Override
                public void onFinished() {
//                    hideDia();
                }
            });
        } else {
            senToa("网络异常");
        }

    }

    public void httpPost(final String type, RequestParams params) {
        if (CheckNetwork.isNetworkAvailable(BaseActivity.this)) {
            if (!type.equals("commit"))
                showDia("正在加载中...");
            x.http().post(params, new Callback.CacheCallback<String>() {
                @Override
                public boolean onCache(String result) {
                    return false;
                }

                @Override
                public void onSuccess(String result) {
                    System.out.println("result" + result);
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("result") == 0) {
                            success(result, type);
                            if (!type.equals("commit"))
                                hideDia();
                        } else
                            error(jsonObject.getString("msg"), type);
                    } catch (JSONException e) {
                        error(e.getMessage(), type);
                        e.printStackTrace();
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    error(ex.getMessage(), type);
                    senToa("error" + ex.getMessage());
                    if (!type.equals("commit"))
                        hideDia();
                }

                @Override
                public void onCancelled(CancelledException cex) {
//                    hideDia();
                }

                @Override
                public void onFinished() {
//                    hideDia();
                }
            });
        } else {
            senToa("网络异常");
        }

    }

    public void success(String result, String type) {

    }

    public void error(String result, String type) {

    }

    public void senToa(String text) {
        Toast mToast = null;
        if (mToast == null) {
            mToast.makeText(context, text, Toast.LENGTH_LONG).show();
        }
    }

    private void showDia(String str) {
        if (progressDialog == null) {
            progressDialog = new ProgressDialog(this);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        progressDialog.setMessage(str);
        progressDialog.show();
    }

    private void hideDia() {
        try {
            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void jumpBundleActvity(Class cls, Bundle bundle) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void jumpActvity(Class cls) {
        Intent intent = new Intent();
        intent.setClass(context, cls);
        startActivity(intent);
    }

    public void jumpBundleActvityforResult(Class cls, Bundle bundle, int code) {
        Intent intent = new Intent();
        intent.putExtras(bundle);
        intent.setClass(context, cls);
        startActivityForResult(intent, code);
    }
}
